﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;

public class TestSpine : MonoBehaviour
{
    SkeletonAnimation m_SM;
    MeshRenderer m_MeshRenderer;
    public Color m_FillColor = Color.black;
    [Range(0f, 1f)]
    public float m_FillPhase = 0f;
    // Start is called before the first frame update
    void Start()
    {
        m_SM = GetComponent<SkeletonAnimation>();
        m_MeshRenderer = GetComponent<MeshRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        m_MeshRenderer.material.SetColor("_FillColor", m_FillColor);
        m_MeshRenderer.material.SetFloat("_FillPhase", m_FillPhase);

        float h = Input.GetAxis("Horizontal");
        if (h < 0)
        {
            transform.localScale = new Vector3(-1f, 1f, 1f);
        }
        else if (h > 0)
        {
            transform.localScale = new Vector3(1f, 1f, 1f);
        }

        string currentAnim = "";
        var track = m_SM.state.GetCurrent(0);
        if (track != null)
        {
            currentAnim = track.Animation.Name;
        }
        if (h != 0)
        {
            if (!currentAnim.Equals("Move"))
                m_SM.state.SetAnimation(0, "Move", true);
        }
        else
        {
            if (!currentAnim.Equals("Idle"))
                m_SM.state.SetAnimation(0, "Idle", true);
        }
    }
}
